/*
* ============================================================================
*  Name        : sea_platform_win.cpp
*  Part of     : Finite state machine framework 
*  Description : 
*  Version     : %version: %
*  
*
* ============================================================================
*/

#include "sea_platform.h"
#include "sea_trace.h"

/**
 * @file sea_platform_win.cpp
 *
 * @ingroup Platforms
 */
using namespace sea;

// -------------------------------------------------------------------------------------------------

int OsThread::m_native_priority[ ] = 
{ 
    THREAD_PRIORITY_IDLE, 
    THREAD_PRIORITY_BELOW_NORMAL, 
    THREAD_PRIORITY_NORMAL, 
    THREAD_PRIORITY_ABOVE_NORMAL, 
    THREAD_PRIORITY_HIGHEST 
};

// -------------------------------------------------------------------------------------------------
// CTor
// -------------------------------------------------------------------------------------------------
OsThread::OsThread( ):
    m_started( false ),
    m_handle( NULL ),
    m_threadId( 0 )
{
}

// -------------------------------------------------------------------------------------------------
// Dtor
// -------------------------------------------------------------------------------------------------
OsThread::~OsThread( ) 
{
    if ( m_started )
    {
        CloseHandle ( m_handle );
    }
}

// -------------------------------------------------------------------------------------------------
// Resume the thread
// -------------------------------------------------------------------------------------------------
void OsThread::start( ) 
{
    if ( !m_started )
    {
        m_handle = CreateThread (
            0,              // Security attributes
            0,              // Stack size
            ( LPTHREAD_START_ROUTINE )&OsThread::entry,
            this,
            CREATE_SUSPENDED,
            &m_threadId );
        
        ASSERT_DEBUG( m_handle );
        m_started = ResumeThread ( m_handle ) != ( DWORD )-1; 
    }
}

// -------------------------------------------------------------------------------------------------
// Wait for the thread to finish
// -------------------------------------------------------------------------------------------------
void OsThread::wait( i::uint32 time /* = -1 */ )
{
    if ( m_started )
    {
        // SEA_TRACE( "Wait for %08x thread to finish", m_threadId );
        WaitForSingleObject( m_handle, time );
    }
}

// -------------------------------------------------------------------------------------------------
// Trace function
// -------------------------------------------------------------------------------------------------
int Trace::Print( const char* format_str, ... )
{
    static OsMutex traceLock;
    OsMutex::Scoped safe( &traceLock );

    int result = -1;
    char buffer[ MAX_BUFFER ];
    String format = format_str;
    format += "\n";

    SYSTEMTIME systemTime;

    GetLocalTime( &systemTime );
    DWORD hour = systemTime.wHour;
    WORD min = systemTime.wMinute;
    WORD sec = systemTime.wSecond;
    WORD msec = systemTime.wMilliseconds;

    result = sprintf_s(
        buffer, MAX_BUFFER, "%02u:%02u:%02u.%03u ", hour, min, sec, msec );
    OutputDebugStringA( buffer );

    va_list arg_list;
    va_start( arg_list, format_str );
    result = _vsnprintf_s(
        buffer, MAX_BUFFER, _TRUNCATE, format.c_str( ), arg_list );
    va_end( arg_list );

    OutputDebugStringA( buffer );
#if 0
    printf( buffer );
    _cprintf( buffer );
#endif

    return result;
}

// -------------------------------------------------------------------------------------------------
